<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<script src="../js/pages/public.js"></script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
						一级分类:<select id="one"></select>
						二级分类:<select id="two"></select>
						三级分类:<select id="three"></select>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="ol">
                        		<li>
		                        	<a href="javascript:select(0)" >顶级分类列表</a>
		                        </li>

	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal"><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="del()"><i class="fa fa-trash-o" ></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">ID</th>
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 增加窗口 -->
  <form id="addForm">
	<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h3 id="myModalLabel">商品分类编辑</h3>
			</div>
			<div class="modal-body">

				<table class="table table-bordered table-striped"  width="800px">
					<tr>
						<td>上级商品分类</td>
						<td>
						   珠宝 >>  银饰
						</td>
					</tr>
					<tr>
						<td>商品分类名称</td>
						<input type="hidden" name="parentId" id="parentId1">
						<td><input  class="form-control" placeholder="商品分类名称" name="name">  </td>
					</tr>
					<tr>
						<td>类型模板</td>
						<td>
							<select id="addTypeList" name="typeId" style="width: 100%"></select>
							</select>
						</td>
					</tr>
				 </table>
			</div>
			<div class="modal-footer">
				<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
				<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			</div>
		  </div>
		</div>
	</div>
  </form>
  <!-- 编辑窗口 -->

<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel1">商品分类编辑</h3>
		</div>
		<div class="modal-body">
			<form id="updateForm">
			<table class="table table-bordered table-striped"  width="800px" id="table">
				<input type="hidden" name="id" id="id">
				<input type="hidden" name="parentId" id="parentId2">
				<tr>
					<td>上级商品分类</td>
					<td>
						珠宝 >>  银饰
					</td>
				</tr>
				<tr>
					<td>商品分类名称</td>
					<td><input  class="form-control" id="name1" placeholder="商品分类名称" name="name">  </td>
				</tr>
				<tr>
					<td>类型模板</td>
					<td>
						<select id="updateTypeList" name="typeId" style="width: 100%"></select>
					</td>
				</tr>
			 </table>
			</form>
		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="update()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

</body>
</html>
<script>
	var parentId = 0;
	var token = window.localStorage.getItem("token");
	$(function() {
	    select(parentId);
		selectType();
	})

	function select(pid,name) {
		$.ajax({
			url:domain+"/itemcat/getByParentId",
			headers : {"AutharizeToken":token},
			type : "POST",
			data:{parentId:pid},
			dataType : "json",
			async : false,
			success : function(result) {
				var list = result.data;
				var  html = '';
				$(list).each(function(i,e) {
					html += '	<tr>'
					html += '	<td><input name="checkbox" value="'+e.id+'" type="checkbox" ></td>'
					html += '	<td>'	+ e.id+     '</td>'
					html += '	<td>'	+ e.parentId+   '</td>'
					html += '	<td>'	+ e.name+'</td>'
					html += '	<td>'	+ e.typeId+'</td>'
					html += '	<td className="text-center" align="center">'
					html += '   <button type="button" onclick="select('+e.id+',\''+e.name+'\')" class="btn bg-olive btn-xs">查询下级</button>'
					html += '	<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="toupdatetype('+e.id+')" >修改</button> '
					html += '	</td>'
					html += '	</tr>'
				})
				$("#tbody").html(html)
				parentId = pid
				$("#parentId1").val(parentId)
				var fenlei = '<li><a href="javascript:select(0)">顶级分类列表</a></li>'
				if (name != undefined) {
					fenlei += '<li><a href="javascript:select(0,'+name+')">'+name+'</a></li>'
				}
				$("#ol").html(fenlei)
			},error : function() {
				alert("系统错误")
			}

		})
	}
	function selectType() {
		//获取条件查询的值
		var parms = {};
		var page = {"pageNum":1,"pageSize":5,"parms":parms};
		$.ajax({
			url: domain+"/TypeTemplateController/typrselect",
			headers : {"AutharizeToken":token},
			contentType: 'application/json;charset=utf-8',
			type:"post",
			dataType:"json",
			//将传到后台的数据转为json格式
			data: JSON.stringify(page),
			//异步请求
			async:false,
			success:function (result) {
				if (result.code==10000){
					console.log(result)
					var list = result.data.rows;
					//拼接路径
					var  html = '';
					html += '<option value="-1">请选择</option>'
					$(list).each(function(i,e) {
						html += '<option value="'+e.id+'">'+e.name+'</option>'
					})
					$("#addTypeList").html(html)
					$("#updateTypeList").html(html)

				}
			},error:function () {
				alert("系统异常")
			}
		})
	}
	//去修改
	function toupdatetype(id) {
		$.ajax({
			url:domain+"/itemcat/getById",
			headers : {"AutharizeToken":token},
			type : "POST",
			data:{id:id},
			dataType : "json",
			/*contentType : "application/json;charset=utf-8",*/
			async : false,
			success : function(result) {
				if (result.code==10000){
					var specvo = result.data;
					console.log(specvo)
					//拼接
					$("#id").val(result.data.id);
					$("#parentId2").val(specvo.parentId);
					$("#name1").val(specvo.name);
					$("#updateTypeList").val(specvo.typeId).trigger("change")
				}
			},error : function() {
				alert("系统错误")
			}
		})
	}

	function add() {
			var parms = $("#addForm").serializeObject();
			$.ajax({
				url: domain+"/itemcat/addCatVO",		  //请求的路径，获取数据源
				headers : {"AutharizeToken":token},
				contentType: 'application/json;charset=utf-8',
				async:false,
				type: "post",			  //请求的方式
				dataType: "json",		  //请求返回的数据类型
				data:JSON.stringify(parms),
				success: function (result) {
					if (result.code==10000){
						alert("增加成功")
						window.location.reload();
					}
				},
				error : function(result) {
					alert("增加失败!")
				}
			})
	}

	function update() {
		var parms = $("#updateForm").serializeObject();
		$.ajax({
			url: domain + "/itemcat/updateCat",
			headers : {"AutharizeToken":token},
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			type: "post",
			async: false,
			data: JSON.stringify(parms),
			success: function (result) {
				if (result.code == 10000) {
					alert("修改成功")
					location.reload();
				}
			}, error: function () {
				alert("系统错误")
			}
		})
	}

	function del() {
		var ids = [];
		var checkbox=$("input[name=checkbox]");
		$(checkbox).each(function (i,v) {
			if ($(v).prop("checked")){
				ids.push($(v).val())
			}
		})
		if (ids.length <= 0) {
			alert("请选择要删除的id");
			return
		}
		if (confirm("是否确定要删除")) {
			$.ajax({
				headers : {"AutharizeToken":token},
				url: domain + "/itemcat/delete?ids=" + ids,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				type: "delete",
				async: false,
				success: function (result) {
					location.reload();
				}, error: function () {
					alert("系统错误")
				}
			})
		}
	}
</script>